package com.zenchn.utils;

/**
 * TrimUtils : 自定义字符串修剪方法
 *
 * @author dingxinfa
 * @date 2018-08-24
 */
public class TrimUtils {
    /**
     * 去掉指定字符串前面和后面指定的字符
     * @param str
     * @param c
     * @return
     */
    public static String trim(String str, char c) {
        char[] chars = str.toCharArray();
        int len = chars.length;
        int st = 0;
        while ( (st < len) && (chars[st] == c) ){
            st ++;
        }
        while ( (st < len) && (chars[len-1] == c) ){
            len --;
        }
        return (st >0) && (len<chars.length)? str.substring(st, len): str;
    }

    /**
     * 去掉指定字符串前面指定的字符
     * @param str
     * @param c
     * @return
     */
    public static String trimLeft(String str, char c) {
        char[] chars = str.toCharArray();
        int len = chars.length;
        int st = 0;
        while ( (st < len) && (chars[st] == c) ){
            st ++;
        }
        return (st >0)? str.substring(st, len): str;
    }

    /**
     * 去掉指定字符串后面指定的字符
     * @param str
     * @param c
     * @return
     */
    public static String trimRight(String str, char c) {
        char[] chars = str.toCharArray();
        int len = chars.length;
        int st = 0;
        while ( (st < len) && (chars[len-1] == c) ){
            len --;
        }
        return (len<chars.length)? str.substring(st, len): str;
    }


    public static void main(String[] args) {
        String str = "000330803103000";
        System.out.println(trim(str, '0'));
        System.out.println(trimLeft(str, '0'));
        System.out.println(trimRight(str, '0'));
    }
}